package com.ms.seguridad;

import com.ms.seguridad.service.DpwcAuthenticationToken;
import com.ms.seguridad.service.SecurityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;

/**
 * User: AW
 * Date: 10/04/13
 */
@Service
public class DpwcAuthenticationProviderImpl extends AbstractUserDetailsAuthenticationProvider {

    @Autowired
    SecurityService securityService;

    @PostConstruct
    public void init() {

    }

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) throws AuthenticationException {

    }

    @Override
    protected UserDetails retrieveUser(String userName, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        DpwcAuthenticationToken tciToken = (DpwcAuthenticationToken) authentication;
        String username = (String) tciToken.getPrincipal();
        String password = (String) tciToken.getCredentials();
        String ipAddress = tciToken.getIpAddress();
        Integer portalId = new Integer((String) tciToken.getPortal());

        return securityService.login(userName,password,ipAddress,4);
    }


    public boolean supports(Class<? extends Object> authentication) {
        return authentication.equals(DpwcAuthenticationToken.class);
    }
}

